package com.billsystem.controller;


import com.billsystem.pojo.Account;
import com.billsystem.pojo.Category;
import com.billsystem.service.AccountService;
import com.billsystem.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
public class AccountController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private HttpSession session;

    @PostMapping("/addAccount")
    public Object addAccount(@RequestBody Account account){
        try{
            int uid = (int)session.getAttribute("userId");
            account.setUid(uid);
            accountService.addAccount(account);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("error");
        }
    }

    @GetMapping("/listAccount")
    public Object listAccount(){
        int uid = (int)session.getAttribute("userId");
        List<Account> lists = accountService.queryListByUid(uid);
        try{
            return Result.success(lists);
        }catch (Exception e){
            return Result.fail("error");
        }
    }

    @PostMapping("/deleteAccount")
    public Object deleteAccount(@RequestBody Account account){
        try{
            int uid = (int)session.getAttribute("userId");
            account.setUid(uid);
            accountService.deleteAccount(account);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("error");
        }
    }

    @PutMapping("/updateAccount")
    public Object updateAccount(@RequestBody Account account){
        try{
            int uid = (int)session.getAttribute("userId");
            account.setUid(uid);
            accountService.updateAccount(account);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("error");
        }
    }

}
